import { defineStore } from 'pinia'
import { ref } from 'vue'

export type AuthMode = 'login' | 'register'

export const useAuthModalStore = defineStore('authModal', () => {
  const visible = ref(false)
  const mode = ref<AuthMode>('login')
  const redirect = ref<string | null>(null)
  const reason = ref<string | null>(null)

  const open = (options?: {
    mode?: AuthMode
    redirect?: string | null
    reason?: string | null
  }) => {
    visible.value = true
    mode.value = options?.mode || 'login'
    redirect.value = options?.redirect ?? redirect.value
    reason.value = options?.reason ?? null
  }

  const openLogin = (redirectPath?: string | null, why?: string | null) => {
    open({ mode: 'login', redirect: redirectPath ?? null, reason: why ?? null })
  }

  const openRegister = (redirectPath?: string | null, why?: string | null) => {
    open({ mode: 'register', redirect: redirectPath ?? null, reason: why ?? null })
  }

  const close = () => {
    visible.value = false
    reason.value = null
  }

  return {
    visible,
    mode,
    redirect,
    reason,
    open,
    openLogin,
    openRegister,
    close,
  }
})

